/**
 * @FileName: DeviceListAdapter.java
 * @Package:com.bdtop.dbl.ui.adapters
 * @Description: TODO
 * @author: MingjunLi
 * @date:2014-9-29
 * @version V1.0
 */

package com.bdtop.dbl.ui.adapters;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bdtop.dbl.DBLApplication;
import com.bdtop.dbl.R;
import com.bdtop.dbl.model.DeviceInfo;
import com.bdtop.dbl.model.Msg;
import com.bdtop.dbl.utils.Constants;

/**
 * @ClassName: MsgListAdapter
 * @Description: TODO
 * @author: liyongqiang
 * @date:2014-10-14
 */
public class MsgListAdapter extends ArrayListAdapter<Msg> {

	public MsgListAdapter(Activity context) {
		super(context);
	}

	@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;

        if (null == row) {
            row = LayoutInflater.from(mContext).inflate(R.layout.listitem_msg, null);

            holder = new ViewHolder();
            holder.icon = (ImageView) row.findViewById(R.id.img_icon);
            holder.content = (TextView) row.findViewById(R.id.tv_content);
            holder.time = (TextView) row.findViewById(R.id.tv_time);
            holder.mdn = (TextView) row.findViewById(R.id.tv_mdn);
            
            row.setTag(holder);
        }
        else {
            holder = (ViewHolder) row.getTag();
        }

        Msg msg = getList().get(position);
        
        String mdn = msg.getMdn();
        if(DBLApplication.getInstance().deviceInfoList != null && !TextUtils.isEmpty(mdn)){
        	for (DeviceInfo di : DBLApplication.getInstance().deviceInfoList) {
        		if(mdn.equals(di.mdn)){
        			mdn = di.name;
        			break;
        		}
			}
        }
        holder.mdn .setText(mdn);
       
        holder.content.setText(msg.getContent());
        holder.time.setText(msg.getTime());
        if (msg.getMtype().equals(Constants.MSGTYPE_SYS)) {
            holder.icon.setBackgroundResource(R.drawable.account);
        } else {
            holder.icon.setBackgroundResource(R.drawable.icon_1);
        }
        return row;
    }

	static class ViewHolder {
		ImageView icon;
		TextView content;
		TextView time;
		TextView mdn;
	}

}
